<#include '../head.html'/>
<div style="width: 450px;margin-top: 20px;">
<form class="layui-form" action="" method="post" id="edit_form">
	<div class="layui-form-item">
    	<label class="layui-form-label">用户名:</label>
    	<div class="layui-input-block">
    	<input type="hidden" name="id" value="${user.id}">
      		<input type="text" name="name" value="${user.name}" required  lay-verify="required" placeholder="用户名" autocomplete="off" class="layui-input">
    	</div>
  	</div>
  	<div class="layui-form-item">
    	<label class="layui-form-label">密码:</label>
    	<div class="layui-input-block">
      		<input type="password" name="password" value="" required  lay-verify="required" placeholder="密码" autocomplete="off" class="layui-input">
    	</div>
  	</div>
	<div class="layui-form-item">
	    <label class="layui-form-label">角色</label>
	    <div class="layui-input-block">
	      <select name="rolegroupid" lay-filter="rolegroupid" lay-verify="required">
	      	<#list roleGroups as roleGroup>
	        <option value=""></option>
	        <option value="${roleGroup.id}"  <#if user.rolegroupid==roleGroup.id >selected="selected"</#if> >${roleGroup.name}</option>
	      	</#list>
	      </select>
	    </div>
	</div>

	<div class="layui-form-item">
	    <label class="layui-form-label">状态</label>
	    <div class="layui-input-block">
	      <input type="checkbox" <#if user.enabled> checked="" </#if> name="enabled" lay-skin="switch" lay-filter="switchEnabled" lay-text="ON|OFF">
	    </div>
	  </div>
  	<div class="layui-form-item">
    	<div class="layui-input-block">
      		<button class="layui-btn" lay-submit="" lay-filter="submit_info">提交</button>
      		<button type="reset" class="layui-btn layui-btn-primary">重置</button>
    	</div>
  	</div>
</form>
</div>
<script src="/statics/layui/layui.js"></script>
<script type="text/javascript">
layui.use(['form', 'layedit', 'laydate'], function(){
	var form = layui.form
  	,layer = layui.layer
  	,layedit = layui.layedit
  	,laydate = layui.laydate;
    form.render();
  	
  	
  //监听提交
    form.on('submit(submit_info)', function(data){
      	$.ajax({
			type : 'POST',
			dataType : "json",
			url : "/user/doCheckName",
			data : $("#edit_form").serialize(),
			cache : false,
			success : function(data) {
				if(data){
					layer.alert("该权限名已经被使用了！");
					return;
				}
				saveUser();
				//location.reload();
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {
				$.messager.progress('close');
				layer.alert(XMLHttpRequest.responseText);
	
			}
		});
      	
      	return false;
    });
  	
});

function saveUser(){
	$.ajax({
		type : 'POST',
		dataType : "json",
		url : "/user/saveEdit.jhtml",
		data : $("#edit_form").serialize(),
		cache : false,
		success : function(data) {
			if(data.state==0){
				layer.alert(
						data.message,
						{
							icon: 1,    // icon
							yes:function(){
								parent.location.reload();
							}
							
						}
				);
			}
		},
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			layer.alert(XMLHttpRequest.responseText);

		}
	});
	
}


$("#btn").click(function() {
	if (!$("#edit_form").form('validate')) {
		layer.alert("输入错误，请检查后重新保存");
		return;
	}
	
	$.ajax({
		type : 'POST',
		dataType : "json",
		url : "/user/doCheckName",
		data : $("#edit_form").serialize(),
		cache : false,
		success : function(data) {
			if(data){
				layer.alert("该权限名已经被使用了！");
				return;
			}
			saveUser();
		},
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			$.messager.progress('close');
			layer.alert(XMLHttpRequest.responseText);

		}
	});
	
});

</script>
